var gulp = require('gulp');
var babel = require('gulp-babel');//把es6语法解析成es5
var concat = require('gulp-concat');//合并
var uglifyjs = require('uglify-js');
var composer = require('gulp-uglify/composer');//压缩
var hash = require('gulp-hash');//对文件名加MD5后缀
var revCollector = require('gulp-rev-collector');//替换路径
var htmlmin = require('gulp-htmlmin'); //压缩html里面的js，css，去除空格
var del = require('del');//删除文件
var minify = composer(uglifyjs, console);
//版本号,每次打包都需要进行版本号更改,以防止md5不会全部进行刷新生成,长度不能超过10,不然gulp-rev-collector无法解析
const _options = {
    algorithm: 'md5',
    hashLength: 10,
    version: '2.0.0508_220418.01'
};

// //js压缩
gulp.task('js', async function () {

    return gulp.src(['./public/js/**/*.js'])
        .pipe(hash(_options))
        .pipe(minify({ compress: { drop_console: true, drop_debugger: true } })).on('error', function (err) {
            console.error('[错误------>]', err.toString())
        })
        .pipe(gulp.dest('./build/public/js'))
        .pipe(hash.manifest('rev-js-manifest.json'))
        .pipe(gulp.dest('./build/json'));
});



//css压缩
var autoprefix = require('gulp-autoprefixer');//兼容处理
var minifyCss = require('gulp-minify-css');//压缩
gulp.task('style', async function () {
    return gulp.src(['./public/css/**/*.css', '!./public/css/iconfont/**/*'])
        .pipe(autoprefix({
            overrideBrowserslist: ['last 2 versions'],
            cascade: false
        }))
        .pipe(minifyCss())
        .pipe(hash(_options))
        .pipe(gulp.dest('./build/public/css'))
        .pipe(hash.manifest('rev-css-manifest.json'))
        .pipe(gulp.dest('./build/json'));
});

//img
gulp.task('images', async function () {
    return gulp.src(['./public/static/images/**/*.jpg', './public/static/images/**/*.png', './public/static/images/**/*.gif', './public/static/images/**/*.svg'], { allowEmpty: true })
        .pipe(hash(_options))//文件名加MD5后缀
        .pipe(gulp.dest('./build/public/static/images'))
        .pipe(hash.manifest('rev-images-manifest.json'))//生成一个rev-manifest.json
        .pipe(gulp.dest('./build/json'));//将 rev-manifest.json 保存到 rev 目录内
});

gulp.task('imageCopy', async function () {
    return gulp.src(['./public/static/images/icon/*.jpg', './public/static/images/icon/*.png'], { allowEmpty: true })
        .pipe(gulp.dest('./build/public/static/images/icon'))

});

gulp.task('data', async function () {
    return gulp.src(['./public/datas/**/*'], { allowEmpty: true })
        .pipe(gulp.dest('./build/public/datas'));
});

gulp.task('iconfont', async function () {
    return gulp.src(['./public/css/iconfont/**/*'], { allowEmpty: true })
        .pipe(gulp.dest('./build/public/css/iconfont'))

});




gulp.task('vendor', async function () {
    return gulp.src(['./public/vendor/**/*'], { allowEmpty: true })
        .pipe(gulp.dest('./build/public/vendor'));
});
gulp.task('themes', async function () {
    return gulp.src(['./public/themes/**/*'], { allowEmpty: true })
        .pipe(gulp.dest('./build/public/themes'));
});

//html压缩
gulp.task('revHtml', async function () {
    var options = {
        removeComments: true,//清除HTML注释
        collapseWhitespace: true,//压缩HTML
        collapseBooleanAttributes: true,//省略布尔属性的值 <input checked="true"/> ==> <input />
        removeEmptyAttributes: true,//删除所有空格作属性值 <input id="" /> ==> <input />
        removeScriptTypeAttributes: true,//删除<script>的type="text/javascript"
        removeStyleLinkTypeAttributes: true,//删除<style>和<link>的type="text/css"
        minifyJS: true,//压缩页面JS
        babel: true,
        minifyCSS: true//压缩页面CSS
    };
    return gulp.src('./html/**/*.html')
        .pipe(htmlmin(options))
        .pipe(gulp.dest('./build/html'))
});


gulp.task('revimg', async function () {
    //css，主要是针对img替换
    return gulp.src(['./build/json/rev-images-manifest.json', './build/css/**/*.css'], { allowEmpty: true })
        .pipe(revCollector({
            replaceReved: true
        }))
        .pipe(gulp.dest('./build/css'));
});
gulp.task('revjs', async function () {
    //css，主要是针对img替换
    return gulp.src(['./build/json/rev-js-manifest.json', './build/js/**/*.js'], { allowEmpty: true })
        .pipe(revCollector({
            replaceReved: true,
        }))
        .pipe(gulp.dest('./build/public/js'));
});

//使用rev替换成md5文件名，这里包括html和css的资源文件也一起
gulp.task('rev', async function () {
    //html，针对js,css,img
    return gulp.src(['./build/json/**/*.json', './build/html/**/*.html'], { allowEmpty: true })
        .pipe(revCollector({
            replaceReved: true
        }))
        .pipe(gulp.dest('./build/html'));
});
gulp.task('processJS', async function () {
    //html，针对js,css,img
    return gulp.src(['./build/json/**/*.json', './build/public/js/**/*.js'], { allowEmpty: true })
        .pipe(revCollector({
            replaceReved: true
        }))
        .pipe(gulp.dest('./build/public/js'));
});

//删除Build文件
gulp.task('clean:Build', async function () {
    return del([
        './build/**/',
    ]);
});


//执行多个任务gulp4的用法 gulp.series()串行，gulp.parallel()并行
gulp.task('default', gulp.series('clean:Build', gulp.parallel('images', 'imageCopy', 'iconfont', 'style', 'data', 'themes', 'vendor', 'revHtml', 'js'), 'revimg', 'revjs', async function () {

}))

gulp.task('build', gulp.parallel('rev', 'processJS'), async function () { })
